<?php

namespace App\Console\Commands;

use App\Models\Mods;
use Illuminate\Console\Command;

class createScriptFileFromMods extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:sh';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '用mods数据生成sh脚本文件';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $str_clear='';
        $str_install='';
//        $mods=Mods::all();
        $mods=Mods::where('status','<>',0)->get();
        foreach ($mods as $mod){
            $str_clear .= <<<___
rm -rf /azerothcore/modules/mod-{$mod->flag}

___;
            if ($mod->status===1){
                $str_install .= <<<___
if [[ \$mods =~ ",{$mod->flag}," ]];then
    git clone {$mod->src_url} --single-branch mod-{$mod->flag}
fi

___;
            }
        }

        echo $str_clear;
        echo $str_install;

        $content_install=file_get_contents(public_path('initAZ/server_install_tmp.sh'));
        $content_update=file_get_contents(public_path('initAZ/server_update_tmp.sh'));

        $content_install=str_replace("###[modsInstall]###",$str_install,$content_install);
        $content_update=str_replace("###[modsClear]###",$str_clear,$content_update);
        $content_update=str_replace("###[modsInstall]###",$str_install,$content_update);

        file_put_contents(public_path('initAZ/server_install.sh'),$content_install);
        file_put_contents(public_path('initAZ/server_update.sh'),$content_update);
        echo '生产成功：server_install.sh,server_update.sh';
    }
}
